<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php

$sql = "select * from tb_membership where id = '".$_GET['member_id']."'";
$qr = select($sql);
$rs= $qr[0];
?>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="15" align="left" class="headmenu"><img src="image/bar_left.gif" width="15" height="42" /></td>
    <td align="left" bgcolor="#434343" class="headmenu">จัดการผู้ใช้งาน-&gt;เพิ่มผู้รับผลประโยชน์ของคุณ<span class="red13">
<?=$rs['name']?> 
    </span>รหัสสมาชิก
<span class="red13"><?=$rs['username']?></span></td>
    <td width="15" align="left" class="headmenu"><img src="image/bar_right.gif" width="15" height="42" /></td>
  </tr>
</table>
<br />
<br />
<?

//*** Add Condition ***//
if($_POST["hdnCmd"] == "Add")
{
	$data = array(
	"membership_id"=>($_POST['member_id2']),
	"ref_id"=>($_POST['ref_id2']),
	"reward"=>$_POST['reward2']
	);
	insert("tb_benefi",$data);
	?>
<SCRIPT LANGUAGE="JavaScript">alert("add data suscessfully");window.location='admin.php?mode=membership_benefi.php&member_id=<?=$_POST['member_id2']?>';</script>	
<?
}

//*** Update Condition ***//
if($_POST["hdnCmd"] == "Update")
{
	$data = array(
	"reward"=>$_POST['reward']
	);
	update("tb_benefi",$data,"id = '".$_POST['benefi_id']."'");
	?>
<SCRIPT LANGUAGE="JavaScript">alert("add data suscessfully");window.location='admin.php?mode=membership_benefi.php&member_id=<?=$_POST['member_id']?>';</script>	
<?
}

//*** Delete Condition ***//
if($_GET["Action"] == "Del")
{
	delete("tb_benefi","id='".$_GET['benefi_id']."'");
	?>
<SCRIPT LANGUAGE="JavaScript">alert("Delete data suscessfully");window.location='admin.php?mode=membership_benefi.php&member_id=<?=$_GET['member_id']?>';</script>	
<?
}
$strSQL = "select * from tb_benefi where membership_id = '".$_GET['member_id']."' ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<form name="frmMain" method="post" action="<?=$_SERVER["PHP_SELF"];?>?mode=membership_benefi.php">
    <input type="hidden" name="hdnCmd" value="">
  <table width="90%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
  <tr bgcolor="#55CB8F" class="text">
    <th height="25" bgcolor="#434343" class="headmenu">รหัส</th>
    <th bgcolor="#434343" class="headmenu">ชื่อ</th>
    <th bgcolor="#434343" class="headmenu">ค่าคอมฯ</th>
    <th colspan="2" bgcolor="#434343" class="headmenu">จัดการ</th>
    </tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
	$sql = "select * from tb_membership where id = '".$objResult['ref_id']."'";
	$qr = select($sql);
	$rs = $qr[0];
?>

  <?
	if($objResult["id"] == $_GET["benefi_id"] and $_GET["Action"] == "Edit")
	{
  ?>
  <tr onmouseover="this.className='menu-over'" onmouseout="this.className='menu'" class="menu">
    <td height="25" align="center" bgcolor="#FFFFFF"><?=$rs['username']?><input type="hidden" name="member_id" size="5" value="<?=$_GET["member_id"];?>" /></td>
    <td align="center" bgcolor="#FFFFFF"><?=$rs['name']?><input type="hidden" name="benefi_id" size="5" value="<?=$objResult["id"];?>" /></td>
    <td align="center" bgcolor="#FFFFFF"><label for="reward"></label>
      <input name="reward" type="text" id="reward" value="<?=$objResult['reward']?>" />
      %</td>
    <td colspan="2" align="right" bgcolor="#FFFFFF"><div align="center">
      <input name="btnAdd" type="button" id="btnUpdate" value="Update" OnClick="frmMain.hdnCmd.value='Update';frmMain.submit();">
	  <input name="btnAdd" type="button" id="btnCancel" value="Cancel" OnClick="window.location='<?=$_SERVER["PHP_SELF"];?>?mode=membership_benefi.php&member_id=<?=$objResult['membership_id']?>';">
    </div></td>
  </tr>
  <?
	}
  else
	{
  ?>
  <tr onmouseover="this.className='menu-over'" onmouseout="this.className='menu'" class="menu">
    <td height="25" align="center" bgcolor="#FFFFFF"><?=$rs["username"];?></td>
    <td align="center" bgcolor="#FFFFFF"><?=$rs['name']?></td>
    <td align="center" bgcolor="#FFFFFF"><?=$objResult['reward']?>%</td>
    <td width="55" align="center" bgcolor="#FFFFFF">
    	<a href="admin.php?mode=membership_benefi.php&Action=Edit&benefi_id=<?=$objResult["id"];?>&member_id=<?=$_GET['member_id']?>"><img src="image/b_edit.gif" border="0" /></a>
    </td>
	<td width="53" align="center" bgcolor="#FFFFFF">
    <a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='<?=$_SERVER["PHP_SELF"];?>?mode=membership_benefi.php&Action=Del&benefi_id=<?=$objResult["id"];?>&member_id=<?=$objResult['membership_id']?>';}"><img src="image/delete.gif" border="0" /></a>
    </td>
  </tr>
  <?
	}
  ?>
<?
}
?>
  <tr onmouseover="this.className='menu-over'" onmouseout="this.className='menu'" class="menu">
    <td height="25" colspan="2" align="center" bgcolor="#eeeeee">
      <label for="select2">
        <select name="ref_id2" id="ref_id2">
        <option selected="selected" value="0">---เพิ่มผู้รับผลประโยชน์---</option>
          <?php
	  	$sql = "select * from tb_membership where level = 4";
		$qr = select($sql);
		$total =count($qr);
		$i=0;
		while($i<$total)
		{
			$rs = $qr[$i];
			$sql_check = "select * from tb_benefi where membership_id = '".$_GET['member_id']."' and ref_id = '".$rs['id']."'";
			$qr_check = select($sql_check);
			$rsc = $qr_check[0];
			$i++;
			if(count($qr_check)==0){
	  ?>
          <option value="<?=$rs['id']?>">ชื่อ ::
            <?=$rs['name']?>
            รหัสสมาชิก ::
            <?=$rs['username']?>
            </option>
          <?php
			}
		}
	  ?>
          </select>
        <input name="member_id2" type="hidden" id="member_id2" value="<?=$_GET['member_id']?>" />
      </label></td>
    <td align="center" bgcolor="#eeeeee"><label for="reward2"></label>
      <input type="text" name="reward2" id="reward2" />
      %</td>
    <td colspan="2" align="right" bgcolor="#eeeeee"><div align="center">
      <input name="btnAdd" type="button" id="btnAdd" value="Add" OnClick="frmMain.hdnCmd.value='Add';frmMain.submit();">
    </div></td>
  </tr>
</table>
</form>

</body>
</html>
